package com.younion.business.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import com.younion.business.IRegioninfoService;
import com.younion.common.paginator.Paginator;
import com.younion.dao.RegioninfoDAO;
import com.younion.vo.RegioninfoVO;

import cpcns.component.common.base.BaseException;
import cpcns.jhsks.util.MapCollectionUtils;

 
public class RegioninfoServiceImpl implements IRegioninfoService {

	protected static final Log logger = LogFactory.getLog(RegioninfoServiceImpl.class);

	private RegioninfoDAO regioninfoDAO;

	public Collection getAll() throws Exception {
		return regioninfoDAO.selectAll();
	}

	public List<RegioninfoVO> getAllProvince() throws Exception {
		return regioninfoDAO.selectAllProvince();
	}

	public RegioninfoVO getById(String id) throws Exception {
		if (StringUtils.isEmpty(id) || Integer.parseInt(id) < 1)
			return null;
		try {
			Map paramMap = new HashMap();
			paramMap.put("id", new Integer(id));
			Object obj = regioninfoDAO.selectById(paramMap);
			return obj == null ? null : (RegioninfoVO) obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(RegioninfoServiceImpl.class.getName()
					+ " >> public RegioninfoVO get(String id) error occur:", e);
		}
	}

	public Collection getByMap(Map paramMap) throws BaseException {
		if (CollectionUtils.isEmpty(paramMap)) {
			return null;
		}
		try {
			return regioninfoDAO.selectByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(RegioninfoServiceImpl.class.getName()
					+ " >> public Collection getByMap(Map map) error occur:", e);
		}
	}

	public int getCountByMap(Map paramMap) throws BaseException {
		try {
			return regioninfoDAO.selectCountByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(RegioninfoServiceImpl.class.getName()
					+ " >> public Collection getCountByMap(Map map) error occur:", e);
		}
	}

	public List get(Map paramMap, Paginator paginator) throws Exception {
		if (MapCollectionUtils.isEmpty(paramMap)) {
			return null;
		}
		try {
			Map map = new HashMap();
			map.put("paramMap", paramMap);
			return regioninfoDAO.selectForPage(map, paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(RegioninfoServiceImpl.class.getName()
					+ " >> public Page (Map paramMap, Paginator paginator) error occur:", e);
		}
	}

	public RegioninfoVO save(RegioninfoVO voObject) throws Exception {
		if (voObject == null)
			return null;
		try {
			Object id = null;
			id = voObject.getId();
			//RegioninfoVO db_voObject = getById(id.toString());
			Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(voObject));
			if (id != null) {
				regioninfoDAO.updateByMap(paramMap);
				return voObject;
			} else {
				return (RegioninfoVO) regioninfoDAO.insertByVo(voObject);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(RegioninfoServiceImpl.class.getName()
					+ " >> public RegioninfoVO save(RegioninfoVO voObject) error occur:", e);
		}
	}

	public Collection save(Collection voList) throws Exception {
		if (CollectionUtils.isEmpty(voList))
			return null;
		try {
			Iterator it = voList.iterator();
			while (it.hasNext()) {
				RegioninfoVO data = (RegioninfoVO) it.next();
				data = save(data);
			}
			return voList;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(RegioninfoServiceImpl.class.getName()
					+ " >> public Collection save(Collection voList) error occur:", e);
		}
	}

	public boolean remove(List ids) throws Exception {
		if (CollectionUtils.isEmpty(ids))
			return false;
		try {
			regioninfoDAO.deleteByIds(ids);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(RegioninfoServiceImpl.class.getName()
					+ " >> public boolean remove(List ids) error occur:", e);
		}
	}

	public boolean removeByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			regioninfoDAO.deleteByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(RegioninfoServiceImpl.class.getName()
					+ " >> public boolean removeByMap(Map paramMap) error occur:", e);
		}
	}

	public void insertVos(List<String[]> txtDataList) throws Exception {
		RegioninfoVO[] regioninfoVOs = new RegioninfoVO[txtDataList.size()];
		for (int i=0;i<txtDataList.size();i++) {
			RegioninfoVO regioninfoVO = new RegioninfoVO();
			String[] strings = txtDataList.get(i);
			regioninfoVO.setRegionId(strings[0]);
			regioninfoVO.setRegion(strings[1]);
			regioninfoVO.setPid(strings[2]);
			regioninfoVOs[i]=regioninfoVO;
		}
		regioninfoDAO.insertVOs(regioninfoVOs);
	}
	
	public Collection getServiceAreaByMap(Map paramMap) throws BaseException{
		try {
			return regioninfoDAO.selectServiceAreaByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(RegioninfoServiceImpl.class.getName()
					+ " >> public Collection getByMap(Map map) error occur:", e);
		}
	}
	
	/**
	 * @param service
	 */
	public void setRegioninfoDAO(RegioninfoDAO regioninfoDAO) {
		this.regioninfoDAO = regioninfoDAO;
	}

	public List<RegioninfoVO> getRegionByIds(List list) throws Exception {
		return this.regioninfoDAO.selectRegioninfoByIds(list);
	}
	
}